//
//  HotwordsHistoryTableView.m
//  danpin
//
//  Created by Hongyu Zhang on 15/11/10.
//  Copyright © 2015年 chuangjia. All rights reserved.
//

#import "HotwordsHistoryTableView.h"
#import "viewconfig.h"

#import "zshare.h"
#import "SearchShare.h"
#import "BaseCell.h"

@implementation HotwordsHistoryTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (NSMutableArray*) historyArray
{
    return [[SearchShare sharedInstance] getHistoryForSearch];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([self historyArray])
    {
        return 3;
    }
    return 2;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        if([ZShare sharedInstance].hotwords)
            return [[ZShare sharedInstance].hotwords count];
        
    }
    else if (section == 1)
    {
        if([ZShare sharedInstance].hotmail)
            return [[ZShare sharedInstance].hotmail count];
    }
    else
    {
        return [[self historyArray] count];
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = [UIColor clearColor];
}


- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
//  Background color
    view.tintColor = [UIColor clearColor];

// Text Color
//   UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
//   [header.textLabel setTextColor:[UIColor whiteColor]];

// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
    sectionView.backgroundColor =[UIColor whiteColor];
    sectionView.alpha = 0.9f;

    UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, (50-30)/2, tableView.frame.size.width, 30)];
    tagValue.backgroundColor = [UIColor clearColor];
    tagValue.textColor = [UIColor grayColor];//UIColorFromRGB(0x000000);
    tagValue.textAlignment = NSTextAlignmentLeft;
    tagValue.font = FONT(12);
    
    if(section == 0)
    {
        tagValue.text = @"热门搜索";
       
    }
    else if(section == 1)
    {
         tagValue.text = @"热门电商网站";
    }
    else
    {
        tagValue.text = @"历史搜索";
    }
    
    [sectionView addSubview:tagValue];
    tagValue=nil;

    return sectionView;
}

- (void)dealloc
{
    if(_actionBlock)
    {
        _actionBlock = nil;
    }
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell ;
    if(indexPath.section ==1)
    {
        cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@"hotmall"];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hotmall"];
            cell.textLabel.font = FONT(13);

        }
        if([ZShare sharedInstance].hotmail)
        {
            if(indexPath.row >= [ZShare sharedInstance].hotmail.count)
            {
                return  cell;
            }
            
            NSDictionary* dc = [[ZShare sharedInstance].hotmail objectAtIndex:indexPath.row];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString * strName = [dc objectForKey:@"name"];
                if(strName && [strName isKindOfClass:[NSString class]])
                {
                    cell.textLabel.text =  strName;//dc objectForKey:@""
                }
            }
        }
    }
    else if(indexPath.section == 0)
    {
        cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@"hotwords"];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hotwords"];
            cell.textLabel.font = FONT(13);


        }
        if([ZShare sharedInstance].hotwords)
        {
            if(indexPath.row >= [ZShare sharedInstance].hotwords.count)
            {
                return  cell;
            }
            
            NSString* strKeyword = [[ZShare sharedInstance].hotwords objectAtIndex:indexPath.row];
            if(strKeyword && [strKeyword isKindOfClass:[NSString class]])
            {
                cell.textLabel.text =  strKeyword;//dc objectForKey:@""
            }
        }
    }
    else
    {
        cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@"history"];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"history"];
            cell.textLabel.font = FONT(13);
            

        }
        

        NSMutableArray* arHistory = [self historyArray];
        if(arHistory && [arHistory isKindOfClass: [NSArray class]])
        {
            if(indexPath.row >= arHistory.count)
            {
                return  cell;
            }
            
            NSDictionary * dc = [arHistory objectAtIndex:indexPath.row];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString* strKey = [dc objectForKey:@"name"];
                if(strKey && [strKey isKindOfClass:[NSString class]])
                {
                    cell.textLabel.textAlignment = NSTextAlignmentLeft;
                    cell.textLabel.text =  strKey;
                }
            }
            else if(dc && [dc isKindOfClass:[NSString class]])
            {
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.textLabel.text = (NSString*) dc;
            }
        }
        
    }

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 1)
    {
        //热门电商入口
        if([ZShare sharedInstance].hotmail)
        {
            NSDictionary* dc = [[ZShare sharedInstance].hotmail objectAtIndex:indexPath.row];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString * strURL = [dc objectForKey:@"url"];
                if(strURL && [strURL isKindOfClass:[NSString class]])
                {
  //                  cell.textLabel.text =  strName;//dc objectForKey:@""
                    if (self.actionBlock) {
                        self.actionBlock(1, strURL);
                    }
                }
            }
        }
    }
    else if(indexPath.section == 0)
    {
        if([ZShare sharedInstance].hotwords)
        {
            if (indexPath.row > [ZShare sharedInstance].hotwords.count) {
                return;
            }
            NSString* strKey = [[ZShare sharedInstance].hotwords objectAtIndex:indexPath.row];
            if(strKey && [strKey isKindOfClass:[NSString class]])
            {
                //   cell.textLabel.text =  strURL;//dc objectForKey:@""
                if (self.actionBlock) {
                    self.actionBlock(2, strKey);
                }
                
            }
        }
    }
    else if(indexPath.section == 2)
    {
        NSMutableArray* arHistory = [self historyArray];
        if(arHistory && [arHistory isKindOfClass: [NSArray class]])
        {
            if (indexPath.row > arHistory.count) {
                return;
            }
            NSDictionary * dc = [arHistory objectAtIndex:indexPath.row];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString* strKey = [dc objectForKey:@"name"];
                if(strKey && [strKey isKindOfClass:[NSString class]])
                {
                    if (self.actionBlock) {
                        self.actionBlock(2, strKey);
                    }
                }
            }
            else if(dc && [dc isKindOfClass:[NSString class]])
            {
                //清除搜索历史
                [[SearchShare sharedInstance] clearHistoryForSearch];
                [self reloadData];
            }
        }
    }
}


- (void) setCallback:(void(^)(int type, NSString* str)) callback
//(void(^actionBlock)(int type, NSString* str)) callback
{
   _actionBlock =  callback;
}


@end
